package cn.foolishbird.hamal.frame.config;

import cn.foolishbird.hamal.frame.util.Utils;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Eric
 * @version 1.0
 * @date 2020-09-29
 */
@Configuration
@EnableSwagger2
@Profile({"dev"})
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        //Swagger2 JWT 整合增加token输入
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build().globalOperationParameters(tokenBuilder("test", "a656a13a-de09-4656-a6a1-32543c728e45"));
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("hamal Swagger API ")
                .description("desc wiki")
                .termsOfServiceUrl("service")
                .contact(new Contact("hamal", "url", "email"))
                .version("1.0")
                .build();
    }

    private List<Parameter> tokenBuilder(String tokenKey, String token) {
        List<Parameter> parameterList = new ArrayList<>();

        ParameterBuilder userIdBuilder = new ParameterBuilder();
        userIdBuilder.name("tokenKey")
                .defaultValue(tokenKey)
                .description("tokenKey")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false).build();
        parameterList.add(userIdBuilder.build());

        String randomNum = Utils.obtainRandomNumber(8);
        ParameterBuilder randomBuilder = new ParameterBuilder();
        randomBuilder.name("randomNum")
                .defaultValue(randomNum)
                .description("随机数")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false).build();
        parameterList.add(randomBuilder.build());

        ParameterBuilder tokenBuilder = new ParameterBuilder();
        tokenBuilder.name("tokenSign")
                .defaultValue(DigestUtils.md5Hex(randomNum + token))
                .description("token签名")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false).build();
        parameterList.add(tokenBuilder.build());
        return parameterList;
    }

}
